home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 882 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.4 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: delete vs incomplete class type
  5. Date: 27 Mar 1996 21:49:38 GMT
  6. Organization: Sun Microsystems Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4jccdg$78i@engnews1.Eng.Sun.COM>
  9. References: <sjcDownA7.6FA@netcom.com>
  10. Reply-To: clamage@Eng.sun.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. X-Nntp-Posting-Host: taumet.eng.sun.com
  13. Content-Length: 2534
  14. X-Lines: 59
  15. Originator: clamage@taumet
  16.  
  17. In article 6FA@netcom.com, sjc@netcom.com (Steven Correll) writes:
  18.  
  19. >What must a conforming compiler do when confronted with this?
  20. >        struct x;
  21. >        struct y;
  22. >        void
  23. >        p(x *arg0, y *arg1)
  24. >          {
  25. >          delete [] arg0;
  26. >          delete arg1;
  27. >          }
  28.  
  29. >Section 5.3.5 of my 5/95 copy of the draft standard says "If the object
  30. >being deleted has incomplete class type at the point of deletion and the
  31. >class has a non-trivial destructor...the behavior is undefined." Is this
  32. >still true?
  33.  
  34. Yes.
  35.  
  36. >I don't see how a compiler could avoid noticing that it hasn't a clue what
  37. >destructor to invoke at compilation time. Is there a reason (aside from
  38. >encouraging people to migrate to Java) not to require the compiler to
  39. >diagnose this as an error, instead of allowing it to fault at execution
  40. >time?
  41.  
  42. If x and y have trivial destructors, the code is well-formed. It would
  43. be possible to make the code unconditionally ill-formed, meaning that
  44. you could not delete an object unless its type is complete. I don't have
  45. a good answer for why this is not the case. (I only see two feasible
  46. definitions: status quo, or the code is always ill-formed.)
  47.  
  48. I would expect a compiler to warn about deleting incomplete types.
  49. The current Sun C++ does, for example.
  50.  
  51. I don't understand the reference to Java. In Java, all objects of class
  52. type are on the heap and are garbage-collected. This has the obvious
  53. advantage of eliminating memory-management errors. On the other hand,
  54. you cannot predict when (or whether, under current Java versions) an
  55. object will ever be destroyed, so doing anything important in a
  56. "destructor" (finalize) seems ill-advised. In effect, it seems to me
  57. that "destructors" in Java should always be trivial, and if cleanup
  58. actions are needed, they should usually be put in a method you call
  59. explicitly at the appropriate time.  Hmmm -- seems isomorphic to the
  60. C++ memory-management problem, except for having somewhat limited bad
  61. consequences. 
  62.  
  63. (OK, the comparison might not be completely fair. Typically, a C++
  64. destructor is needed to free allocated memory, and Java never needs a
  65. destructor for that purpose. But if the destructor is needed to free
  66. some other resource -- close a file, release a lock -- you are back
  67. to the "when does it happen" problem. Future versions of Java are
  68. supposed to guarantee that the "finalize" method of every object is
  69. eventually run, but you cannot predict or control the order in which
  70. they are run.)
  71.  
  72. ---
  73. Steve Clamage, stephen.clamage@eng.sun.com
  74.  
  75.  
  76.  
  77.  
  78. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  79. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  80. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  81. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  82. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  83.